function addMedicineItem() {
    var medicineId      =   $('#addMedicineFrm #medicineId').val();
    var medicineName    =   $.trim($('#addMedicineFrm #medicineName').val());
    if (medicineId == '' || medicineName == 0) {
        showCustomDialog(_('Please enter medicine name'), 'medicineName');
        return;
    }
    var lcInt = locale(decimalPoint, thousandSep, 0);

    // Quantity must be > 1
    if (!validatePositiveIntegerByLocale($('#medicineQuantity'), lcInt, true, '', 'dummy')) {
        showCustomDialog(_('Quantity must be a interger > 1'), 'medicineName');
        return;
    }

    // Price must be  > 0
    if (!validatePositiveIntegerByLocale($('#medicineUnitPrice'), lcInt, true, '', 'dummy')) {
        showCustomDialog(_('Price must be > 0'), 'medicineUnitPrice');
        return;
    }   
    submitAddMedicine();
}

function submitAddMedicine() {    
    $.ajax({
        type:   'POST',
        url:    '../InsurancePayment/addPaymentItemAjax?set_ajax_view',
        data:   $('form#addMedicineFrm').serialize() + 
        		'&ticketId=' + $('#ticketId').val() +
        		'&itemListRender=renderItemList',                              
        dataType: 'json',
        beforeSend: function() {  
            showAjaxLoader('addMedicineLoader', 'addMedicineBtnDiv', true);        
        },
        complete: function() {
            showAjaxLoader('addMedicineLoader', 'addMedicineBtnDiv', false);         
        },
        success: function(data) {
            if (data.error.code == 'OK') {
                setDataToElements({itemList: 'serviceListDiv', paymentSummary: 'paymentSummaryDiv'}, data.result);
                resetAddMedicineItemForm();
                $('#medicineName').focus();
            }
            else {
                displayError(data.error.mess);
            }
            
        }       
    });
}

function resetAddMedicineItemForm() {
    $('#addMedicineFrm #medicineName').val('');
    $('#addMedicineFrm #medicineId').val('');
    $('#addMedicineFrm #medicineQuantity').val(1);
    $('#addMedicineFrm #medicineUnitPrice').val('');
    $('#addMedicineFrm #medicinePatientPrice').text('');
    $('#addMedicineFrm #medicineOriginalPrice').text('');
    $('#addMedicineFrm #medicineOriginalPrice').text('');
    $('#medicineUnitLbx option:1st').select();
    $('#unitTitle').val($('#medicineUnitLbx option:1st').attr('title'));
}

function deleteMedicineItem(insServiceId) {
    var name    =   $('#serviceName_' + insServiceId).text();
    var message =   "<span style='color: red; font-weight: bold'>" + name + "</span>";                   
    promptConfirm(_('Delete medicine'), message, function() {
        $.ajax({
            type:   'POST',
            url:    '../InsurancePayment/deletePaymentItemAjax?set_ajax_view',
            data:   'paymentItemId=' + insServiceId +
                    '&itemListRender=renderItemList' ,
            dataType: 'json',
            beforeSend: function() {
                showAjaxLoader(insServiceId + '_loader', insServiceId + '_btn', true);
            },
            success: function(data) {
                if (data.error.code == 'OK') {
                    setDataToElements({itemList: 'serviceListDiv', paymentSummary: 'paymentSummaryDiv'}, data.result);
                    resetAddMedicineItemForm();
                    $('#addServiceFrm #serviceCode').focus();
                }
                else {
                    displayError(data.error.mess);
                }
            }       
        });
        return true;
    },'YesNo', 'warn' ,_('No'));        
} 
